#!/bin/bash -xe

dir=/opt/kata/share/defaults/kata-containers/
prefix="configuration-"
suffix=".toml"

usage () {
  set +x
  echo -e "\033[31m
Usage: $0 <runtimeclass> [debug]

       runtimeclass: valid value is shown as below
         kata-qemu
	 kata-qemu-coco-dev
	 kata-qemu-sev
	 kata-qemu-csv
	 kata-qemu-csv2
	 kata-qemu-csv3
       debug:
         1 means enable debug
	 0 means disable debug
      \033[0m"
  set -x
}

case $1 in
"kata-qemu")
  runtime_tag="qemu"
  ;;
"kata-qemu-coco-dev")
  runtime_tag="qemu-coco-dev"
  ;;
"kata-qemu-sev")
  runtime_tag="qemu-sev"
  ;;
"kata-qemu-csv")
  runtime_tag="qemu-csv"
  ;;
"kata-qemu-csv2")
  runtime_tag="qemu-csv2"
  ;;
"kata-qemu-csv3")
  runtime_tag="qemu-csv3"
  ;;
*)
  echo --- INVALID RuntimeClass ---
  usage
  exit 1
  ;;
esac
enable_disable=${2:-1}

config=$dir/${prefix}${runtime_tag}${suffix}

if [ $enable_disable -ne 0 ]; then
sudo sed -i "s/^#enable_debug = .*$/enable_debug = true/g" $config
sudo sed -i "s/^#debug_console_enabled = .*$/debug_console_enabled = true/g" $config
sudo sed -i "s/^\(level = \"\)\(\"\)$/\1debug\2/g" /etc/containerd/config.toml
else
sudo sed -i "s/^\(enable_debug = .*$\)/#\1/g" $config
sudo sed -i "s/^\(debug_console_enabled = .*$\)/#\1/g" $config
sudo sed -i "s/^\(level = \"\)debug\(\"\)$/\1\2/g" /etc/containerd/config.toml
fi

sudo systemctl daemon-reload
sudo systemctl restart containerd
